package cn.gbase.jiangsu.data.transfer.util;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


/** *//**
 * @author liuwei
 * 
 */
public class ImageHelper{

    private static final String bmp = "bmp";
    private static final String png = "png";
    private static final String gif = "gif";
    private static final String jpeg = "jpeg";
    private static final String jpg = "jpg";
    public static final String thumbnail = "thumbnail.jpg";

    public static void zoompicture(
            String source, 
            int width, 
            int height,
            boolean adjustsize,String target){
        if (source == null || source.equals("") || width < 1 || height < 1) {
            return;
        }
        source = source.toLowerCase();
        if (source.endsWith(bmp)){
            bmpthumbnailhandler(source, width, height, adjustsize,target);
        } else if (source.endsWith(png) || source.endsWith(gif)
                || source.endsWith(jpeg) || source.endsWith(jpg)) {
            thumbnailhandler(source, width, height, adjustsize,target);
        }
    }

    private static void thumbnailhandler(
            String source, 
            int width, 
            int height,
            boolean adjustsize,String target) {
        try {
            File sourcefile = new File(source);
            if (sourcefile.exists()) {
                Image image = ImageIO.read(sourcefile);
                int theimgwidth = image.getWidth(null);
                int theimgheight = image.getHeight(null);
                int[] size = { theimgwidth, theimgheight };
                if (adjustsize) {
                    size = adjustimagesize(theimgwidth, theimgheight, width,height);
                }
                StringBuffer thumbnailfile=new StringBuffer();
                thumbnailfile.append(sourcefile.getParent());
                thumbnailfile.append(File.separatorChar);
                thumbnailfile.append(target);
                writefile(image, size[0], size[1], thumbnailfile.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    private static void bmpthumbnailhandler(
            String source, 
            int width,
            int height,
            boolean adjustsize,String target) {
        try {
            File sourcefile = new File(source);
            if (sourcefile.exists()) {
                Image image = getbmpimage(source);
                int theimgwidth = image.getWidth(null);
                int theimgheight = image.getHeight(null);
                int[] size = { theimgwidth, theimgheight };
                if (adjustsize) {
                    size = adjustimagesize(theimgwidth, theimgheight, width, height);
                }
                StringBuffer thumbnailfile=new StringBuffer();
                thumbnailfile.append(sourcefile.getParent());
                thumbnailfile.append(File.separatorChar);
                thumbnailfile.append(target);
                writefile(image, size[0], size[1], thumbnailfile.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    private static Image getbmpimage(String source) throws Exception {

    	FileInputStream fs = null;
        Image image = null;
        try {
            fs = new FileInputStream(source);
            int bflen = 14;
            byte bf[] = new byte[bflen];
            fs.read(bf, 0, bflen); // 读取14字节bmp文件头
            int bilen = 40;
            byte bi[] = new byte[bilen];
            fs.read(bi, 0, bilen); // 读取40字节bmp信息头

            // 源图宽度
            int nwidth = (((int) bi[7] & 0xff) << 24)
                    | (((int) bi[6] & 0xff) << 16)
                    | (((int) bi[5] & 0xff) << 8) | (int) bi[4] & 0xff;

            // 源图高度
            int nheight = (((int) bi[11] & 0xff) << 24)
                    | (((int) bi[10] & 0xff) << 16)
                    | (((int) bi[9] & 0xff) << 8) | (int) bi[8] & 0xff;

            // 位数
            int nbitcount = (((int) bi[15] & 0xff) << 8) | (int) bi[14] & 0xff;

            // 源图大小
            int nsizeimage = (((int) bi[23] & 0xff) << 24)
                    | (((int) bi[22] & 0xff) << 16)
                    | (((int) bi[21] & 0xff) << 8) | (int) bi[20] & 0xff;

            // 对24位bmp进行解析
            if (nbitcount == 24) {
                int npad = (nsizeimage / nheight) - nwidth * 3;
                int ndata[] = new int[nheight * nwidth];
                byte brgb[] = new byte[(nwidth + npad) * 3 * nheight];
                fs.read(brgb, 0, (nwidth + npad) * 3 * nheight);
                int nindex = 0;
                for (int j = 0; j < nheight; j++) {
                    for (int i = 0; i < nwidth; i++) {
                        ndata[nwidth * (nheight - j - 1) + i] = (255 & 0xff) << 24
                                | (((int) brgb[nindex + 2] & 0xff) << 16)
                                | (((int) brgb[nindex + 1] & 0xff) << 8)
                                | (int) brgb[nindex] & 0xff;
                        nindex += 3;
                    }
                    nindex += npad;
                }
                Toolkit kit = Toolkit.getDefaultToolkit();
                image = kit.createImage(new MemoryImageSource(nwidth, nheight,
                        ndata, 0, nwidth));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        } finally {
            if (fs != null) {
                fs.close();
            }
        }
        return image;
    }

    private static void writefile(
            Image image, 
            int width, 
            int height,
            String thumbnailfile) throws Exception {
        
        if (image == null) return;
        
        BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(image, 0, 0, width, height, null);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(thumbnailfile);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(tag);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        } finally {
            if (out != null) {
                out.close();
            }
        }

    }

    private static int[] adjustimagesize(int theimgwidth, int theimgheight,
            int defwidth, int defheight) {        
        int[] size = { 0, 0 };
        
        float theimgheightfloat=Float.parseFloat(String.valueOf(theimgheight));
        float theimgwidthfloat=Float.parseFloat(String.valueOf(theimgwidth));
        if (theimgwidth<theimgheight){
            float scale=theimgheightfloat/theimgwidthfloat;
            size[0]=Math.round(defheight/scale);
            size[1]=defheight;
        }else{
            float scale=theimgwidthfloat/theimgheightfloat;
            size[0]=defwidth;
            size[1]=Math.round(defwidth/scale);
        }
        return size;
    }

    public static void main(String[] agrs) {
//        zoompicture("C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/AllImage/image/20130225/c042c11b3e6044d98ecfd0e800426273.jpg", 520,480 , true,"temp_c042c11b3e6044d98ecfd0e800426273.jpg");
    Double p=new Double("7600");
    int gold=(int) (p/10000);
    int silver=(int) (p%10000)/100;
    int cl=(int) (p%10000)%100;
//    System.out.println("gold : "+gold);
//    System.out.println("silver : "+silver);
//    System.out.println("cl : "+cl);
    }

}

